﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL;
using Sunimage.Spjg.BLL.Helper;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Common.Exceptions;
using Sunimage.Spjg.Web.Helper;

namespace Sunimage.Spjg.Web.UserClient
{
    public partial class File : System.Web.UI.Page
    {
        FileBl fileBl = new FileBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();


            }
        }

        private void LoadData()
        {
            gvUsers.DataSource = fileBl.GetAll(GsjUserHelper.UserId);
            gvUsers.DataBind();
        }

        protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //点击编辑
            if (e.CommandName == "EditUser")
            {
                Guid id = Guid.Parse(e.CommandArgument.ToString());
                Response.Redirect("FileEdit.aspx?id=" + id);

            }
            //点击删除
            else if (e.CommandName == "DeleteUser")
            {
                if (GsjUserHelper.Permissions.Split(',').Contains(((int)PermissionEnum.FileDelete).ToString()))
                {
                    Guid id = Guid.Parse(e.CommandArgument.ToString());
                    try
                    {
                        fileBl.Delete(id);
                        LoadData();
                        MessageHelper.RegShowJSAjax(gvUsers, "删除文件成功", this.Page);
                    }
                    catch (ReferDataNotExistException)
                    {
                        AlertHelper.ShowError("该文件正在被使用，不允许删除！", this.Page);
                    }
                }
                else
                {
                    AlertHelper.ShowError("您没有删除该文件的权限！", this.Page);
                }
            }
            else if (e.CommandName == "Download")
            {
                if (GsjUserHelper.Permissions.Split(',').Contains(((int)PermissionEnum.FileDownload).ToString()))
                {
                    Guid id = Guid.Parse(e.CommandArgument.ToString());
                    DiskFile file = fileBl.GetById(id);
                    FileUpDownHelper.Download(file.Title, file.Path);
                }
                else
                {
                    AlertHelper.ShowError("您没有下载该文件的权限！", this.Page);
                }
            }
        }

        protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewHelper.AddRowNumber(e);
        }

        protected void btnSaveNewCommercer_Click(object sender, EventArgs e)
        {
            if (GsjUserHelper.Permissions.Split(',').Contains(((int)PermissionEnum.FileUpload).ToString()))
            {
                if (FileUpload1.HasFile && FileUpload1.FileContent.Length > 0)
                {
                    string path = FileUpDownHelper.Upload(FileUpload1, fileBl.GetFileDirectory(Server.MapPath("~"), GsjUserHelper.UserId),
                                                      System.IO.Path.GetRandomFileName(), FileUpload1.FileName);

                    //
                    DiskFile file = new DiskFile();
                    file.Title = FileUpload1.FileName;
                    file.Path = path;
                    file.UploadTime = DateTime.Now;
                    file.Remark = tbxRemark.Text.Trim();
                    file.OwnerId = GsjUserHelper.UserId;
                    fileBl.Create(file);

                    LoadData();
                    MessageHelper.Show("上传成功!", Page);
                }
                else
                {
                    MessageHelper.Show("请选择文件!", Page);
                }
            }
            else
            {
                AlertHelper.ShowError("您没有上传文件的权限！", this.Page);
            }
        }

        protected void pager_PageChanged(object sender, EventArgs e)
        {
            gvUsers.PageIndex = pager.CurrentPageIndex - 1;
            LoadData();
        }
    }
}